package com.rz.admin.dubbo.service;

import com.alibaba.fastjson.JSONArray;
import com.rz.admin.entity.SysCountry;
import com.rz.admin.service.ICountryService;
import consumer.CountryService;
import consumer.model.Country;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CountryServiceImpl
 * @Description 国家/地区service实现类
 * @Author maweiyue
 * @Date 2019/3/26 20:37
 */
@Service(version = "1.0.0")
public class CountryServiceImpl implements CountryService {

    @Resource
    private ICountryService countryService;

    /**
     * 根据国家代码获取国家信息列表
     * @param code
     * @return
     */
    @Override
    public List<Country> listCountryByCode(String code) {
        List<SysCountry> list = countryService.listCountryByCode(code);
        return buildList(list);
    }

    /**
     * 根据国家代码获取国家信息列表
     * @param name
     * @return
     */
    @Override
    public List<Country> listCountryByName(String name){
        List<SysCountry> list = countryService.listCountryByName(name);
        return buildList(list);
    }

    /**
     * 根据国家代码获取国家信息列表
     * @param codeList
     * @return
     */
    @Override
    public List<Country> listCountryByCode(List<String> codeList) {
        List<SysCountry> list = countryService.listCountryByCode(codeList);
        return buildList(list);
    }

    /**
     * 获取所有国家列表
     * @return
     */
    @Override
    public List<Country> listAllCountry(){
        List<SysCountry> list = countryService.listAllCountry();

        return buildList(list);
    }

    private List<Country> buildList(List<SysCountry> list){
        List<Country> result = new ArrayList<>();
        list.forEach(sysCountryEntity -> {
            Country country = new Country();
            BeanUtils.copyProperties(sysCountryEntity,country);
            result.add(country);
        });
        return result;
    }

    /**
     * 获取所有国家列表，并组成key-value集合，以便页面下拉列表访问
     * @return
     */
    @Override
    public JSONArray selectCountries() {
        return countryService.selectCountries();
    }
}
